package com.notepad.service;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.notepad.util.LockTask;

import java.util.Timer;

public class LockService extends Service {
    private final static int FOREGROUND_ID = 0;
    private Timer mTimer;
    public LockService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        startForeground(FOREGROUND_ID,new Notification());
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startTimer();
        return super.onStartCommand(intent, flags, startId);
    }

    private void startTimer() {
        if(mTimer == null){
            mTimer = new Timer();
            LockTask lockTask = new LockTask(this);
            mTimer.schedule(lockTask,0L,1000L);
        }

    }

    @Override
    public void onDestroy() {
        stopForeground(true);
        mTimer.cancel();
        mTimer.purge();
        mTimer = null;
        super.onDestroy();

    }
}
